/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package issr.boundary;

import com.vaadin.data.Item;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import issr.boundary.vaadin.AbstractView;
import issr.boundary.vaadin.MainApplication;
import issr.controller.ControlloreInserimentoTipoProdotto;
import issr.entity.Prodotto;
import issr.entity.Utente;
import issr.misc.Labels;
import java.util.ArrayList;

/**
 *
 * @author Soulreaver
 */
public class PannelloInserimentoTipoProdotto extends AbstractView implements Button.ClickListener {

    private ControlloreInserimentoTipoProdotto controllore;
    private final TextField nomeField = new TextField(Labels.NOME);
    private final Button submit;
    private final Form form = new Form();
    private final FormLayout flayout;
    private final Table textTable;
    private final VerticalLayout layout;

    public PannelloInserimentoTipoProdotto() {
        controllore = new ControlloreInserimentoTipoProdotto();
        layout = new VerticalLayout();
        layout.setHeight("100%");
        layout.setWidth("100%");
        layout.setSpacing(true);
        layout.setMargin(true);
        textTable = new Table();
        textTable.addContainerProperty(Labels.DATA_INSERIMENTO, String.class, null);
        textTable.addContainerProperty(Labels.NOME, String.class, null);
        setSizeFull();
        setCompositionRoot(layout);

        flayout = new FormLayout();
        setSizeFull();
        flayout.setHeight("100%");
        flayout.setWidth("100%");
        flayout.setSpacing(true);
        flayout.setMargin(true);

        nomeField.setRequired(true);
        form.addField(Labels.NOME, nomeField);
        flayout.addComponent(form);
        
        submit = new Button("Submit", this);
        submit.setClickShortcut(KeyCode.ENTER);
        flayout.addComponent(submit);
        layout.addComponent(flayout);
        layout.addComponent(textTable);
    }

    public void visualizzaCatalogo() {
        ArrayList<Prodotto> prodotti = controllore.ottieniCatalogo(((Utente) MainApplication.getProject().getUser()).getPuntovendita());

        for (int dummyId = 0; dummyId < prodotti.size(); dummyId++) {
            Item item = textTable.addItem(dummyId);
            item.getItemProperty(Labels.DATA_INSERIMENTO).setValue(prodotti.get(dummyId).getInserimento());
            item.getItemProperty(Labels.NOME).setValue(prodotti.get(dummyId).getNome());
        }

        textTable.sort(new Object[]{Labels.DATA_INSERIMENTO}, new boolean[]{false});
        textTable.setSizeFull();
        textTable.setPageLength(0);
        textTable.setImmediate(true);
        textTable.setColumnWidth(Labels.DATA_INSERIMENTO, 160);
    }

    @Override
    protected void close() throws Exception {
    }

    @Override
    protected void open() throws Exception {
        resetForm();
        visualizzaCatalogo();
    }

    protected void resetForm() {
        textTable.removeAllItems();
        nomeField.setValue("");
    }

    public void buttonClick(ClickEvent event) {
        if (!((String) nomeField.getValue()).equals("")) {
            if (controllore.inserisciNuovoProdotto(
                    (String) nomeField.getValue(),
                    (Utente) MainApplication.getProject().getUser(),
                    ((Utente) MainApplication.getProject().getUser()).getPuntovendita()) == true) {
                MainApplication.getProject().getMainWindow().showNotification(
                        "Prodotto " + (String) nomeField.getValue() + " creato con successo");
            }
        } else {
            MainApplication.getProject().getMainWindow().showNotification(
                    "Errore inserimento prodotto");
        }
    }
}
